from django.shortcuts import render,redirect
from django.http import HttpResponse,JsonResponse
from django.views import View
from rest_framework.response import Response
from rest_framework.views import APIView
# Create your views here.
from functools import wraps
from django.utils.decorators import method_decorator
from .serve import *
import re
from django_test.settings import UPLOAD_ROOT
import os



class Login(APIView):
    def post(self,request):
        username = request.data['username']
        password = request.data['password']
        users = User.objects.filter(username=username,password=password).first()
        if users:
            return Response({
                'code': 200,
                'data': users.id
            })
            
        else:
            return Response({
                'code': 400
            })


# 展示商品
class AllGoods(APIView):
    def post(self,request):
        good = Goods.objects.all()
        good_ser = GoodsSer(good,many=True)
        return Response({
            'code': 200,
            'data': good_ser.data
        })

# 预定
class Show(APIView):
    def post(self,request):
        username = request.data.get('username')
        total = request.data.get('total')
        order = Orders.objects.create(username=username,total=total)
        order.save()

        return Response({
            'code': 200,
            'message':'提交成功'

        })

# 展示购物车
class AllOrder(APIView):
    def post(self,request):
        order = Orders.objects.all()
        order_ser = OrdersSer(order,many=True)
        return Response({
            'code': 200,
            'data': order_ser.data
        })








